package com.hspedu.newData.dmsxl.数组.二分查找;

import org.junit.Test;

import java.util.*;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-12 
 * @Version: 1.0
 */

public class LC2089找出数组排序后的目标下标 {

    public List<Integer> targetIndices(int[] nums, int target) {

        Arrays.sort(nums);

        List<Integer> list = new ArrayList<>();
        int left = 0;
        int right = nums.length - 1;

        while (left <= right){
            int mid = left + (right - left)/2 ;


            if (nums[mid] == target){
                int i = mid;
                int j = mid-1;
                while (i <= nums.length - 1 && nums[i] == target){
                    list.add(i++);
                }
                while (j>= 0 && nums[j] == target){
                    list.add(j--);
                }
                break;
            } else if (nums[mid] > target) {
                right = mid-1;
            }else {
                left = mid+1;
            }
        }
        Collections.sort(list);
        return list;
    }

    @Test
    public void test() {
        // 测试代码
        targetIndices(new int[]{1,2,5,2,3},5);

    }
}
